#! /usr/bin/env python

import pygame
from pygame.locals import *

from data import *
from sprites import *

class Level:
    def __init__(self, player, lvl=1):
        self.level      = pygame.image.load(filepath(("level%d.png" % lvl))).convert()
        self.rect       = self.level.get_rect()
        self.sprites    = []
        self.x = 0
        self.y = 0
        for y in range(self.level.get_height()):
            self.y = y
            for x in range(self.level.get_width()):
                self.x = x
                color = self.level.get_at((self.x, self.y))
                if color == (0, 0, 0, 255):
                    Grass((self.x*32, self.y*32))
                if color == (255, 200, 0, 255):
                    Brick((self.x*32, self.y*32))
                if color == (0, 38, 255, 255):
                    Cannon((self.x*32, self.y*32), player)
                if color == (87, 0, 127, 255):
                    Cloud((self.x*32, self.y*32))
                if color == (0, 255, 8, 255):
                    Finish((self.x*32, self.y*32))
                if color == (255, 72, 0, 255):
                    FinishLine((self.x*32, self.y*32))
                if color == (0, 255, 255, 255):
                    Cannon2((self.x*32, self.y*32), player)
                    
    def get_at(self, dx, dy):
        try:
            return self.level.get_at((self.x+dx, self.y+dy))
        except:
            pass
            
    def get_size(self):
        return [self.level.get_size()[0]*32, self.level.get_size()[1]*32]
        
    def get_rect(self):
        return self.rect
        
    def get_surface(self):
        return pygame.Surface(self.rect.size)
        
    def get_sprites(self):
        return self.sprites